#include <fstream>
#include <iostream>
#include <thread>
#include <chrono>  // 可选的头文件，用于提供 sleep_for 函数
#include <signal.h>
using namespace std;

#define SIGKILL     9   
// 定义一个全局变量来记录是否收到 SIGKILL 信号
volatile sig_atomic_t sig_kill_received = 0;
// SIGKILL 信号处理函数
void sig_kill_handler(int signum) {
    sig_kill_received = 1;
}

class Logger {
public:
    Logger() {
        logFile_.open("log.txt", ios::out);
    }
    ~Logger() {
        if (logFile_.is_open()) {
            logFile_.close();
        }
    }

    void writeLog(int index) {
        logFile_ << index << endl;
    }

private:
    ofstream logFile_;
};


void gameLoop() {
    Logger logger;
    int index = 0;
    while (!sig_kill_received) {
        index++;
        logger.writeLog(index);
        std::this_thread::sleep_for(std::chrono::seconds(5));
    }
    logger.writeLog(-1);
}

int main() {
    // 注册 SIGKILL 信号处理函数
    signal(SIGKILL, sig_kill_handler);
    thread t(gameLoop);
    t.join();
    return 0;
}